/****************************************************
    文件：EditScriptTemplate.cs
	作者：HuskyT
    邮箱：1005240602@qq.com
    日期：2021/07/03 13:20:15
	功能：编辑脚本模板
*****************************************************/

using System.Text;
using HTFramework.Extension;

namespace HTFramework.Utility.Editor
{
    public class EditScriptTemplate
    {
        /// <summary>
        /// 编辑脚本模板
        /// </summary>
        /// <param name="writeHead">是否写入脚本头信息</param>
        /// <param name="data">脚本模板数据</param>
        /// <param name="className">类名</param>
        /// <returns></returns>
        public static string EditCodeTemplate(ScriptTemplateData data, string className)
        {
            if (data.ScriptTemplate.IsNullOrEmpty() || data.UsingIndex > data.ScriptTemplate.Count - 1) return "";

            ScriptTemplateDataItem item = data.ScriptTemplate[data.UsingIndex];

            StringBuilder sb = new StringBuilder();

            //Head
            if (data.WriteHead)
                EditorUtil.EditHeadInfo(ref sb, className, data.User.ToString(), data.Mail.ToString());
            //Using
            if (item.UsingList.IsNullOrEmpty() == false)
            {
                for (int i = 0; i < item.UsingList.Count; i++)
                {
                    EditorUtil.EditUsing(ref sb, item.UsingList[i]);
                }

                sb.AppendLine();
            }

            //命名空间
            EditorUtil.EditNameSpace(ref sb, item.NameSpace);
            sb.AppendLine("{"); //namespace
            //类名
            EditorUtil.EditClassName(ref sb, className, GetInherit(item));
            sb.AppendLine("\t{"); //class
            //方法
            if (item.FuncList.IsNullOrEmpty() == false)
            {
                FuncData funcData;
                for (int i = 0; i < item.FuncList.Count; i++)
                {
                    funcData = item.FuncList[i];
                    EditorUtil.EditFunction(ref sb, funcData.IsPublic, funcData.IsStatic, funcData.ReturnString,
                        funcData.FunctionName, funcData.ParamList, funcData.InnerInfo);
                }
            }
            else
            {
                sb.AppendLine();
            }

            sb.AppendLine("\t}"); //class
            sb.AppendLine("}"); //namespace
            return sb.ToString();
        }

        /// <summary>
        /// 编辑脚本头信息
        /// </summary>
        /// <returns></returns>
        public static string EditHeadInfo(string className, string userName, string mail)
        {
            StringBuilder sb = new StringBuilder();
            EditorUtil.EditHeadInfo(ref sb, className, userName, mail);
            return sb.ToString();
        }

        /// <summary>
        /// 获取继承信息
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static string GetInherit(ScriptTemplateDataItem item)
        {
            string inheritStr;
            if (item.Inherit == string.Empty)
            {
                inheritStr = item.IsMono ? "MonoBehaviour" : "";
            }
            else
            {
                inheritStr = item.IsMono ? $"MonoBehaviour, {item.Inherit}" : item.Inherit;
            }

            return inheritStr;
        }
    }
}